﻿using System;
using System.IO;
using Microsoft.Extensions.Configuration;

namespace _092.DeleteUnwantedFiles {
    internal class Program {
        static void Main(string[] args) {
            string configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");

            if (!File.Exists(configFilePath)) {
                CreateDefaultConfig(configFilePath);
            }

            // 创建 ConfigurationBuilder 实例
            IConfigurationBuilder builder = new ConfigurationBuilder()
                .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            // 构建 IConfiguration 实例
            IConfiguration configuration = builder.Build();

            // 读取配置项
            string filesPath = configuration.GetSection("FilesPath").Value;
            bool delVbf = configuration.GetValue<bool>("delVbf");
            int vbfFileSize = configuration.GetValue<int>("VbfFileSize");

            // 删除
            RemoveUnusedFiles(filesPath, 0, delVbf, vbfFileSize);
            Console.WriteLine("删除完成！");
            Console.ReadKey();
        }

        /// <summary>
        /// 递归删除文件夹
        /// </summary>
        /// <param name="folderPath"></param> 文件路径
        /// <param name="currentLevel"></param> 文件夹层数
        /// <param name="delVbf"></param> 是否删除大于10M的vbf
        /// <param name="size"></param> 大小
        private static void RemoveUnusedFiles(string folderPath, int currentLevel = 0, bool delVbf = false, int size = 10) {
            if (!Directory.Exists(folderPath)) {
                return;
            }

            if (delVbf) {
                string[] files = Directory.GetFiles(folderPath);
                foreach (string file in files) {
                    string extension = Path.GetExtension(file);
                    if (extension == ".vbf") {
                        long fileSizeInBytes = new FileInfo(file).Length;
                        double fileSizeInMB = fileSizeInBytes / (1024.0 * 1024.0);
                        if (fileSizeInMB > size) {
                            File.Delete(file);
                        }
                    }
                }
            }

            string[] directories = Directory.GetDirectories(folderPath);
            if (currentLevel <= 2) {
                foreach (string directory in directories) {
                    RemoveUnusedFiles(directory, currentLevel + 1, delVbf, size);
                }
            } else {
                Directory.Delete(folderPath, true);
            }
        }

        /// <summary>
        /// 创建默认的配置文件
        /// </summary>
        /// <param name="configFilePath"></param>
        public static void CreateDefaultConfig(string configFilePath) {
            // 创建默认配置内容
            string defaultConfig = @"
        {
            ""FilesPath"": ""D:\\defaultFiles"",
            ""delVbf"": false,
            ""VbfFileSize"": 10
        }";

            // 将默认配置内容写入新的配置文件
            File.WriteAllText(configFilePath, defaultConfig);
        }
    }
}